Prelab

·        To save and send debug information like logs, I implemented 3 functions to append, print, and reset the logs. The printLog() takes care of sending records&logs to the computer through Bluetooth, 1 log at a time; The resetLog() clear all the log lists content; The appendLog() append new entries to the log lists.

o   resetLog

A screen shot of a computer code

Description automatically generated

o   appendLog

o   A screenshot of a computer program

Description automatically generated

o   printLog

o   A screenshot of a computer program

Description automatically generated

 

 

Lab

1.     Measuring ToF sensor returning frequency & PID parameters

o   Use the command GET_ToF defined in lab2 to measure the average data collection frequency.

o   As the graph shows, the average data collection frequency is 1599/(915.227-759.694)=10.28

§  A screenshot of a computer

Description automatically generated

 

2.      Tunning the parameters kp, ki, kd by try and error.

o   My set point is 1 feet from wall, and the car starts at 1.5 meters from the wall.

o   Kp, Ki, Kd are the coefficient of P, I, D, the calculation is:

A close-up of a white background

Description automatically generated

where curerror is the distance from current position to set point, integration is the integration of curerror with a  cap defined as

A white background with blue text

Description automatically generated ,

and pos_t_gradient is gradient of real_position-against-time curve defined as

pos_t_gradient = (float)(currealpos - prerealpos) / (currealtime - prerealtime);

o   Any nonzero motor input u that’s in the range of -45~+45 is strengthened to +/-45 to overcome the deadband at 30; setting it to 45 rather than 30 to ensure the car can actually move on various ground with different friction; Any nonzero motor input u that’s out of motor’s range -255~+255 is limited to +/-255.

§  A screenshot of a computer code

Description automatically generated

o   Kp influences the velocity of the car according to the distance to the set point. Increase kp from 0 and observed that the car started to oscillate around the set point when kp=135.

o   Next step, set ki to 0 since any non-zero ki made the car target at a point over the set point, or even hit the wall if the point is behind the wall.

o    Kd penalizes high velocity. Increase the kd from 0, and observed that the car stopped hitting the wall before settling at set point when kd=50.

o   The position and Motor control U=P+I+D versus time with kp, ki, kd=135, 0, 50:

§  A graph with numbers and lines

Description automatically generated

§  A graph of a line graph

Description automatically generated

 

3.     Sampling rate & Extrapolation

o   As observed in step1, although I have removed all the serial print statements, the Time of Flight sensors are still as slow as 10.28 readings per seconds. This is much slower than the clock speed of Artemis Nano which is 180 as measured in Lab3, so I let Artemis continue to run even when theres no new ToF readings. This is when the extrapolation take place: when the Artemis board is running without the new data from Time of Flight sensors, it increments the car's position by dt*pos_t_gradient, where dt is the time interval of the board loop just finished, and pos_t_gradient is gradient of real_position-against-time curve; when there is an up-to-date ToF reading, it sets the car's current position equal to the reading, and calculate the pos_t_gradient with the time differences and distance differences between this and previous ToF readings. This helps the car predict its current position without the real time reading from ToF sensors.

§  A screen shot of a computer code

Description automatically generated

o   To get the highest possible frequency, I did not include any print, Bluetooth transition, or blocking statements in the chip’s main loop. This ensures the car has the highest possible reaction speed.

4.     Car controlling

o   The running of car is controlled by 2 boolean: running and allowrun. running is  running = (curtime < startT + milliduration); where milliduration is a value passed in with the CMD.PID_CONTROL_STOP_ON_WALL command using ble.send_command. Running will be set to false once the car has run for the speficied time length. allowrun is initialized to true every time CMD.PID_CONTROL_STOP_ON_WALL command is sent, and it is set to false if CMD. STOP_RUNNING command is sent: this enables me to forcibly stop the car without chasing it.

o   setVelocity(left, right) is implemented so that I can set the car’s velocity in one line without considering which motor should be analogWrite to nonzero based on the sign of expected velocity.

§  A screenshot of a computer code

Description automatically generated

5.     Low pass filter

o   My D term is a little bit fuzzy, but I did not think an LPF was necessary for my car because the noise was too low to overcome the cars inertia to cause an observable impact.

6.     Derivative kick

o   Derivative kick is resolved by removing the first data point from the dataset; in other words, the graph excludes the data points with the derivative value that is not ready to use.

7.     Highest velocity

o   as the graph shows, the highest velocity my car achieved is 2.1 m/s. highest velocity